<template>
  <div class="relative w-full h-full px-4 bg-light-400 jeesite-login">
    <div class="container relative h-full py-2 mx-auto">
      <div class="flex h-full">
        <div
          class="hidden min-h-full pl-4 mr-4"
          style="display: flex; flex-direction: column; width: 54.166667%"
        >
          <div class="my-auto" style="margin-left: 15%">
            <AppLogo class="logo" />
            <img alt="6zw" src="@/assets/svg/login-box-bg.svg" class="w-1/2" />
            <div class="mt-10 font-medium text-white">
              <span class="inline-block mt-4 text-3xl">
                最好用的快速开发平台</span
              >
            </div>
            <div class="text-context">
              JeeSite 是一个专业的平台，是一个让你使用放心的平台。<br />
              前端基于 Vue3、Vite、TypeScript、Ant-Design-Vue、Vben Admin，<br />
              后台基于 Spring Boot、Apache MyBatis
              等，最先进、最经典的技术栈。<br />
              精致的
              UI、规范的代码书写、匠心著作、封装细节、专注业务、快速开发。<br />
            </div>
          </div>
        </div>
        <div class="my-top-level-div">
          <el-form
            ref="form"
            :model="loginForm"
            :rules="loginRules"
            label-position="left"
            label-width="80px"
            class="form-box"
          >
            <h3 class="title">英语学习系统</h3>
            <el-form-item prop="username">
              <el-input
                v-model="loginForm.username"
                type="text"
                size="large"
                auto-complete="off"
                placeholder="账号"
              >
                <template #prefix
                  ><svg-icon
                    icon-class="user"
                    class="el-input__icon input-icon"
                /></template>
              </el-input>
            </el-form-item>
            <el-form-item prop="password">
              <el-input
                v-model="loginForm.password"
                type="password"
                size="large"
                auto-complete="off"
                placeholder="密码"
                @keyup.enter="handleLogin"
              >
                <template #prefix
                  ><svg-icon
                    icon-class="password"
                    class="el-input__icon input-icon"
                /></template>
              </el-input>
            </el-form-item>
            <el-form-item prop="code" v-if="captchaEnabled">
              <el-input
                v-model="loginForm.code"
                size="large"
                auto-complete="off"
                placeholder="验证码"
                style="width: 63%"
                @keyup.enter="handleLogin"
              >
                <template #prefix
                  ><svg-icon
                    icon-class="validCode"
                    class="el-input__icon input-icon"
                /></template>
              </el-input>
              <div class="login-code">
                <img :src="codeUrl" @click="getCode" class="login-code-img" />
              </div>
            </el-form-item>
            <el-checkbox
              v-model="loginForm.rememberMe"
              style="margin: 0px 0px 25px 0px"
              >记住密码</el-checkbox
            >
            <el-button
              style="margin: 0px 0px 6px 15px"
              size="small"
              @click="toQrLogin"
              >二维码登录</el-button
            >
            <el-form-item style="width: 100%">
              <el-button
                :loading="loading"
                size="large"
                type="primary"
                style="width: 100%"
                @click.prevent="handleLogin"
              >
                <span v-if="!loading">登 录</span>
                <span v-else>登 录 中...</span>
              </el-button>
              <div style="float: right" v-if="register">
                <router-link class="link-type" :to="'/register'"
                  >立即注册</router-link
                >
              </div>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </div>
  </div>
</template>
  <script  setup>
import { getCodeImg, getQrCode, checkQrCode } from "@/api/login";
import Cookies from "js-cookie";

import { encrypt, decrypt } from "@/utils/jsencrypt";
import useUserStore from "@/store/modules/user";
import { setToken } from "@/utils/auth";
const userStore = useUserStore();
const router = useRouter();
const { proxy } = getCurrentInstance();
const QRcodeSrc = ref("");
const QRTimer = ref(null);
const loginForm = ref({
  username: "",
  password: "",
  rememberMe: false,
  code: "",
  uuid: "",
});

const loginRules = {
  username: [{ required: true, trigger: "blur", message: "请输入您的账号" }],
  password: [{ required: true, trigger: "blur", message: "请输入您的密码" }],
  code: [{ required: true, trigger: "change", message: "请输入验证码" }],
};

const QRcodeSwitch = ref(false);
const codeUrl = ref("");
const loading = ref(false);
// 验证码开关
const captchaEnabled = ref(true);
// 注册开关
const register = ref(false);
const redirect = ref(undefined);

function handleLogin() {
  proxy.$refs.loginRef.validate((valid) => {
    if (valid) {
      loading.value = true;
      // 勾选了需要记住密码设置在 cookie 中设置记住用户名和密码
      if (loginForm.value.rememberMe) {
        Cookies.set("username", loginForm.value.username, { expires: 30 });
        Cookies.set("password", encrypt(loginForm.value.password), {
          expires: 30,
        });
        Cookies.set("rememberMe", loginForm.value.rememberMe, { expires: 30 });
      } else {
        // 否则移除
        Cookies.remove("username");
        Cookies.remove("password");
        Cookies.remove("rememberMe");
      }
      // 调用action的登录方法
      userStore
        .login(loginForm.value)
        .then(() => {
          console.log("跳转首页");
          router.push({ path: redirect.value || "/index" });
        })
        .catch(() => {
          loading.value = false;
          // 重新获取验证码
          if (captchaEnabled.value) {
            getCode();
          }
        });
    }
  });
}

function toQrLogin() {
  QRcodeSwitch.value = !QRcodeSwitch.value;
  getQrCode().then((res) => {
    console.log(res);
    QRcodeSrc.value = res.img;
    let uuid = res.uuid;
    QRTimer.value = setInterval(function () {
      checkQrCode({
        uuid,
      }).then((ans) => {
        console.log(ans);
        if (ans == null) {
        } else {
          clearInterval(QRTimer.value);
          setToken(ans.token);
          router.push({ path: redirect.value || "/" });
        }
      });
    }, 2000);
  });
}

function getCode() {
  getCodeImg().then((res) => {
    captchaEnabled.value =
      res.captchaEnabled === undefined ? true : res.captchaEnabled;
    if (captchaEnabled.value) {
      codeUrl.value = res.img;
      loginForm.value.uuid = res.uuid;
    }
  });
}

function getCookie() {
  const username = Cookies.get("username");
  const password = Cookies.get("password");
  const rememberMe = Cookies.get("rememberMe");
  loginForm.value = {
    username: username === undefined ? loginForm.value.username : username,
    password:
      password === undefined ? loginForm.value.password : decrypt(password),
    rememberMe: rememberMe === undefined ? false : Boolean(rememberMe),
  };
}

getCode();
getCookie();
</script>
  <style lang="less">
@prefix-cls: ~"jeesite-login";
@logo-prefix-cls: ~"jeesite-app-logo";
@countdown-prefix-cls: ~"jeesite-countdown-input";
@dark-bg: #293146;
.title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #707070;
}
.form-box {
  width: 500px;
  padding: 20px;
  border-radius: 15px;
  background-color: #fff;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
}
.my-top-level-div {
  position: fixed;
  position: absolute;
  left: 60%;
  top: 20%;
  z-index: 9999;
  /* 这里可以添加其他样式属性 */
}
.text-context {
  --tw-text-opacity: 1;
  color: rgba(255, 255, 255, var(--tw-text-opacity));
  margin-top: 1.25rem;
  font-weight: 400;
}
.text-3xl {
  font-size: 1.875rem;
  line-height: 2.25rem;
}
.inline-block {
  display: inline-block;
}
.mt-4 {
  margin-top: 1rem;
}
.font-medium {
  font-weight: 500;
}
.text-white {
  --tw-text-opacity: 1;
  color: rgba(255, 255, 255, var(--tw-text-opacity));
}
.mt-10 {
  margin-top: 2.5rem;
}
.w-1\/2 {
  width: 50%;
}
.my-auto {
  margin-top: auto;
  margin-bottom: auto;
}
.w-full {
  width: 100%;
}
.relative {
  position: relative;
}
.px-4 {
  padding-left: 1rem;
  padding-right: 1rem;
}
.h-full {
  height: 100%;
}

.pl-4 {
  padding-left: 1rem;
}
.bg-light-400 {
  --tw-bg-opacity: 1;
  background-color: rgba(246, 246, 246, var(--tw-bg-opacity));
}
.mr-4 {
  margin-right: 1rem;
}
.min-h-full {
  min-height: 100%;
}
html[data-theme="dark"] {
  .@{prefix-cls} {
    background-color: @dark-bg;

    &::before {
      background-image: url(/@/assets/svg/login-bg-dark.svg);
    }

    .ant-input,
    .ant-input-password {
      background-color: #232a3b;
    }

    .ant-btn:not(.ant-btn-link):not(.ant-btn-primary) {
      border: 1px solid #4a5569;
    }

    &-form {
      background: transparent !important;
      box-shadow: none;
    }

    .app-iconify {
      color: #fff;
    }
  }

  input.fix-auto-fill,
  .fix-auto-fill input {
    -webkit-text-fill-color: #c9d1d9 !important;
    box-shadow: inherit !important;
  }
}

.@{prefix-cls} {
  min-height: 100%;
  overflow: hidden;

  @media (max-width: 992px) {
    background-color: #3f60b5;

    .@{prefix-cls}-form {
      box-shadow: none;
    }
  }

  &-form {
    top: -20px;
    margin: auto;
    background-color: #fff;
    box-shadow: 0 0 8px #ddd;
  }

  &::before {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    margin-left: -48%;
    background-image: url(@/assets/svg/login-bg.svg);
    background-position: 100%;
    background-repeat: no-repeat;
    background-size: auto 100%;
    content: "";
    @media (max-width: 992px) {
      display: none;
    }
  }

  .@{logo-prefix-cls} {
    // position: absolute;
    // top: 12px;
    // height: 30px;

    &.logo {
      margin-top: -110px;
      padding-bottom: 80px;
    }

    &__title {
      font-size: 16px;
      color: #eee;
    }

    img {
      width: 32px;
    }
  }

  .container {
    .@{logo-prefix-cls} {
      display: flex;
      width: 60%;
      height: 80px;

      &__title {
        font-size: 28px;
        color: #eee;
      }

      img {
        width: 48px;
      }
    }
  }

  &-sign-in-way {
    .anticon {
      font-size: 22px;
      color: #888;
      cursor: pointer;

      &:hover {
        color: blue;
      }
    }
  }

  input:not([type="checkbox"]) {
    min-width: 360px;

    @media (max-width: 1200px) {
      min-width: 320px;
    }

    @media (max-width: 992px) {
      min-width: 260px;
    }

    @media (max-width: 768px) {
      min-width: 240px;
    }

    @media (max-width: 576px) {
      min-width: 160px;
    }
  }

  .valid-code input {
    min-width: auto;
  }

  .@{countdown-prefix-cls} input {
    min-width: unset;
  }

  .ant-divider-inner-text {
    font-size: 12px;
    color: black;
  }
}
</style>
  